package com.easylinkin.linkappapi.function.controller;


import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.function.entity.LinkappFunctionRefDeviceType;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.function.entity.FunctionRefPrivilegeDTO;
import com.easylinkin.linkappapi.function.entity.LinkappFunction;
import com.easylinkin.linkappapi.function.service.LinkappFunctionService;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tongjie
 * @since 2020-05-16
 */
@RestController
@RequestMapping("/function")
public class LinkappFunctionController {


  @Resource
  private LinkappFunctionService service;


  @PostMapping("/add")
  @CommonOperateLogAnnotate(module = LogModule.FUNCTION, desc = "新增功能")
  public RestMessage add(@RequestBody @Valid LinkappFunction function) {
    return RestBuilders.successBuilder(service.add(function)).build();
  }

  @PostMapping("/update")
  @CommonOperateLogAnnotate(module = LogModule.FUNCTION, desc = "修改功能")
  public RestMessage update(@RequestBody LinkappFunction function) {
    return RestBuilders.successBuilder(service.updateLinkappFunction(function)).build();
  }


  @PostMapping("/deleteBatch")
  @CommonOperateLogAnnotate(module = LogModule.FUNCTION, desc = "批量删除功能")
  public RestMessage deleteBatch(@RequestBody List<LinkappFunction> functionList) {
    Assert.notEmpty(functionList, "参数为空");
    return RestBuilders.successBuilder(service.deleteBatch(functionList)).build();
  }

  @PostMapping("/getLinkappFunctions")
  public RestMessage getLinkappFunctions(@RequestBody LinkappFunction function) {
    Assert.notNull(function, "function 不能为空");
    return RestBuilders.successBuilder().data(service.getLinkappFunctions(function)).build();
  }

  @PostMapping("/getPage")
  public RestMessage getLinkappFunctionsPage(
      @RequestBody RequestModel<LinkappFunction> requestModel) {
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<LinkappFunction> record = service
        .getLinkappFunctionsPage(requestModel.getPage(), requestModel.getCustomQueryParams());
    return RestBuilders.successBuilder().data(record).build();
  }

  @PostMapping("/getLinkappFunction")
  public RestMessage getLinkappFunction(@RequestBody LinkappFunction function) {
    Assert.notNull(function, "function 不能为空");
    return RestBuilders.successBuilder().data(service.getLinkappFunction(function)).build();
  }

  @GetMapping("/selectPrivilegeByFunction/{functionId}")
  public RestMessage selectPrivilegeByApplication(@PathVariable String functionId) {
    return RestBuilders.successBuilder().data(service.selectPrivilegeByFunction(functionId))
        .build();
  }

  @PostMapping("/function2Privileges")
  @CommonOperateLogAnnotate(module = LogModule.FUNCTION, desc = "功能关联权限")
  public RestMessage function2Privileges(
      @RequestBody FunctionRefPrivilegeDTO functionRefPrivilegeDTO) {
    service.function2Privileges(functionRefPrivilegeDTO);
    return RestBuilders.successMessage();
  }

    /**
     * 根据功能id查询关联的设备类型
     * @param functionId
     * @return
     */
  @GetMapping("/getRefDeviceType/{functionId}")
  public RestMessage getRefDeviceType(@PathVariable String functionId) {
    return RestBuilders.successBuilder().data(service.getRefDeviceType(functionId))
        .build();
  }

  @PostMapping("/saveOrUpdateDeviceType")
  public RestMessage saveOrUpdateDeviceType(@RequestBody LinkappFunctionRefDeviceType functionRefDeviceType) {
    Assert.notNull(functionRefDeviceType.getFunctionId(), "functionId 不能为空");
    return RestBuilders.successBuilder().data(service.saveOrUpdateDeviceType(functionRefDeviceType))
        .build();
  }

}
